package com.cameras;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;

class Loadthread extends Thread {
	private Bitmap mBitmap;
	private int mX = 0;
	private int mY = 0;
	private int mKeyCode = -1;
	private SurfaceHolder mSurfaceHolder;
	private boolean mRun = false;
	private int mSec;
	private int mSpeed;
	static int dir = 0;

	public Loadthread(SurfaceHolder holder,
			Bitmap bitmap,int mSec) {
		mSurfaceHolder = holder;
		mBitmap = bitmap;
		mSpeed = 35;
		this.mSec = mSec;
	}
	public void setSpeed(int sp) {
		mSpeed = sp;
	}

	public void setRunning(boolean b) {
		mRun = b;
	}

	public void doKeyDown(int keyCode, KeyEvent msg) {
		mKeyCode = keyCode;
	}

	public void doKeyUp(int keyCode, KeyEvent msg) {
		mKeyCode = -1;
	}

	Handler mHandler = null;

	private void animate(){
		Canvas c = null;
		int fps = 0;
		long cur = System.currentTimeMillis();
		long last = System.currentTimeMillis();
		float degree = 0;
		long cur1 = 0;
		float phase = 0;
		
		while (mRun) {
			try {
				synchronized (mSurfaceHolder) {
					c = mSurfaceHolder.lockCanvas(null);
					cur1 = System.currentTimeMillis();
					float elapsedt = (float) (cur1 - last) / 1000;
					last = cur1;
					Paint paint = new Paint();
					Rect rect = mSurfaceHolder.getSurfaceFrame();
					c.drawBitmap(mBitmap, new Rect(0, 0, mBitmap.getWidth(),
							mBitmap.getHeight()), new RectF(0, 0, rect.width(),
							rect.height()), new Paint());
					paint.setColor(Color.GREEN);

					paint.setStyle(Style.STROKE);
					c.translate(rect.centerX(), rect.centerY());

					phase += mSpeed * elapsedt;
					DashPathEffect effect = new DashPathEffect(new float[] { 9 , 5}, phase);
					if (phase >= 100)
						phase = 0;
					paint.setPathEffect(effect);
					paint.setStrokeWidth(2.8f);
					c.drawRoundRect(new RectF(-35, -30, 35, 30), 5, 5, paint);
				
					onDraw(c);

				}
				fps++;

			} finally {
				if (c != null)
					mSurfaceHolder.unlockCanvasAndPost(c);
			}
			fps++;

			long now = System.currentTimeMillis();
			if (now - cur > 1000) {
				mSec = (mSec + 15) % 16;
				if(mSec == 0)Looper.myLooper().quit();
				Log.d("KZK", "FPS=" + (fps * 1000 / ((double) now - cur)));
				fps = 0;
				cur = now;
			}
		}
	}

	public void run() {
		Looper.prepare();
		Handler handler = new Handler();
		handler.post(new Runnable(){
			public void run()
			{
				animate();
			}
		});
		Looper.loop();
	}

	protected void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		paint.setColor(Color.GREEN);
		paint.setAntiAlias(true);
		paint.setTextAlign(Paint.Align.CENTER);
		paint.setTextSize(36);
		// canvas.drawBitmap(image, 0, 0, null);
		if (mKeyCode == KeyEvent.KEYCODE_DPAD_UP)
			mSpeed++;
		if (mKeyCode == KeyEvent.KEYCODE_DPAD_DOWN)
			mSpeed--;
		float txtsize = paint.getTextSize();
		canvas.drawText(mSec + "s", 0, txtsize / 2, paint);

	}
}
